<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Requests\SysConfigRequest;
use App\Models\SysConfig;
use Illuminate\Http\Request;

class SysConfigController extends ApiBaseController
{
    public function index(Request $request){
        $paginator = SysConfig::filter($request->all())->orderBy('id', 'desc')->paginate($this->pageSize);
        return success(page_format($paginator));
    }

    public function show(Request $request, SysConfig $model){
        $id = $request->get('id');
        $data = $model->find($id);
        return success(['data' => $data->toArray()]);
    }

    public function create(SysConfigRequest $request){
        $params = $request->all();
        $model = SysConfig::create($request->all());
        return success([],'添加成功');
    }

    public function update(SysConfigRequest $request, SysConfig $model)
    {
        $id = $request->post('id');
        $dept = $model->find($id);
        $validated = $request->validated();

        if($dept){
            $dept->update($request->all());
        }else{
            return error('数据不存在');
        }

        return success([],'更新成功');
    }

    public function destroy($ids, Request $request, SysConfig $model){
        $id_arr = explode(',', $ids);
        $model->destroy($id_arr);
    }

    public function getValue($config_key, SysConfig $model){
        $data = $model->where('config_key', trim($config_key))->first();
        return success(['data' => $data['config_value']],$data['config_value']);
    }

    public function refreshCache(){
        //todo
        return success([],'todo');

    }
}
